package com.my.ruyi.test;

import java.util.List;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.CombinedChannelDuplexHandler;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;

/**
 * 入站--解码器
 * 出站--编码器
 *
 */
public class CombinedIntegerStringCodec extends CombinedChannelDuplexHandler<StringToIntegerDecoder, IntegerToStringEncoder>{
	public CombinedIntegerStringCodec() {
		super(new StringToIntegerDecoder(), new IntegerToStringEncoder());
	}
}

// 编码器
class IntegerToStringEncoder extends MessageToMessageEncoder<Integer>{

	// 处理出站消息
	@Override
	protected void encode(ChannelHandlerContext ctx, Integer msg, List<Object> out) throws Exception {
		
		out.add(msg.toString());
	}
}

// 解码器
class StringToIntegerDecoder extends MessageToMessageDecoder<String>{
	// 处理入站消息
	@Override
	protected void decode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
		out.add(Integer.valueOf(msg));
	}
}